package org.example.sys

import android.app.Application
import android.util.Log
import com.google.gson.Gson
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.example.sys.service.BookApi
import org.jetbrains.annotations.NotNull
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import java.util.concurrent.TimeUnit


class App : Application() {

    companion object {
        lateinit var instance: App
        val httpClient = OkHttpClient.Builder()
            .connectTimeout(8, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
        val retrofit: Retrofit = Retrofit.Builder()
            .baseUrl("http://192.168.1.100:555/api/v1/")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .client(httpClient.build())
            .build()

        val bookApi = retrofit.create<BookApi>()
    }

    init {
        instance = this
    }



    override fun onCreate() {
        super.onCreate()
        val httpLoggingInterceptor = HttpLoggingInterceptor {
            @Override
            fun log(@NotNull message: String) {
                println(message)
            }
        }
        httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
        httpClient.addInterceptor(HttpLoggingInterceptor())
    }

}
